package com.nbclass.modules.news.controller;

import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbclass.common.util.PageUtil;
import com.nbclass.common.util.StringUtils;
import com.nbclass.common.util.UUIDUtil;
import com.nbclass.common.vo.AjaxResultVo;
import com.nbclass.modules.base.BaseController;
import com.nbclass.modules.news.model.NewsArticle;
import com.nbclass.modules.news.model.NewsCategory;
import com.nbclass.modules.news.service.INewsArticleService;
import com.nbclass.modules.news.service.INewsCategoryService;

/**
 * 新闻文章表 前端控制器
 */
@Controller
@RequestMapping("/newsArticle")
public class NewsArticleController extends BaseController {

	@Autowired
	private INewsArticleService newsArticleService;

	@Autowired
	private INewsCategoryService newsCategoryService;

	/**
	 * 文章管理--文章列表
	 */
	@GetMapping("/toNewsArticlePage")
	public String newsarticleList() {
		return "newsarticle/list";
	}

	/**
	 * 文章列表数据
	 * 
	 * @param newsArticle
	 * @param limit
	 * @param offset
	 * @return
	 */
	@RequiresPermissions("newsArticle:list")
	@PostMapping("/list")
	@ResponseBody
	public AjaxResultVo pageArticle(NewsArticle newsArticle, Integer limit, Integer offset) {
		try {
			PageHelper.startPage(PageUtil.getPageNo(limit, offset), limit);
			List<NewsArticle> articleList = newsArticleService.listNewsArticle(newsArticle);
			PageInfo<NewsArticle> pageResult = new PageInfo<>(articleList);
			AjaxResultVo resutlVo = AjaxResultVo.success();
			resutlVo.put("rows", pageResult.getList());
			resutlVo.put("total", pageResult.getTotal());
			return resutlVo;
		} catch (Exception e) {
			logger.error(String.format("文章列表查询失败 NewsArticleController.pageArticle%s", e));
			return AjaxResultVo.error("查询失败");
		}
	}

	/**
	 * 跳转新增文章
	 * 
	 * @param model
	 * @return
	 */
	@RequiresPermissions("newsArticle:add")
	@GetMapping("/add")
	public String add(Model model) {
		QueryWrapper<NewsCategory> queryWrapper = new QueryWrapper<NewsCategory>();
		queryWrapper.eq("status", 1);
		List<NewsCategory> categoryList = newsCategoryService.list(queryWrapper);
		model.addAttribute("categoryList", categoryList);
		return "newsarticle/add";
	}

	/**
	 * 插入新增文章
	 * 
	 * @param newsArticle
	 * @return
	 */
	@RequiresPermissions("newsArticle:add")
	@PostMapping("/doAdd")
	@ResponseBody
	public AjaxResultVo addArticle(NewsArticle newsArticle) {
		try {
			if (StringUtils.isBlank(newsArticle.getTitle())) {
				return AjaxResultVo.error("文章标题不能为空！");
			}
			if (StringUtils.isBlank(newsArticle.getBrief())) {
				return AjaxResultVo.error("文章摘要不能为空！");
			}
			if (StringUtils.isBlank(newsArticle.getContent())) {
				return AjaxResultVo.error("文章内容不能为空！");
			}
			if (StringUtils.isBlank(newsArticle.getBannerPic())) {
				return AjaxResultVo.error("文章图片不能为空！");
			}
			Date nowDate = new Date();
			newsArticle.setNewsArticleId(Long.valueOf(UUIDUtil.getUniqueIdByUUId()));
			newsArticle.setCreateTime(nowDate);
			newsArticle.setUpdateTime(nowDate);
			if (newsArticleService.save(newsArticle)) {
				return AjaxResultVo.success("添加文章成功");
			} else {
				return AjaxResultVo.error("添加文章失败");
			}
		} catch (Exception e) {
			logger.error(String.format("文章添加失败 NewsArticleController.addArticle%s", e));
			return AjaxResultVo.error("添加文章失败");
		}
	}

	/**
	 * 编辑文章详情
	 * 
	 * @param model
	 * @param newsArticleId
	 * @return
	 */
	@RequiresPermissions("newsArticle:edit")
	@GetMapping("/edit")
	public String detail(Model model, String newsArticleId) {
		QueryWrapper<NewsCategory> queryWrapper = new QueryWrapper<NewsCategory>();
		queryWrapper.eq("status", 1);
		List<NewsCategory> categoryList = newsCategoryService.list(queryWrapper);
		model.addAttribute("categoryList", categoryList);
		NewsArticle newsArticle = newsArticleService.getById(newsArticleId);
		model.addAttribute("newsArticle", newsArticle);
		return "newsarticle/detail";
	}

	/**
	 * 编辑文章
	 * 
	 * @param newsArticle
	 * @return
	 */
	@RequiresPermissions("newsArticle:edit")
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResultVo editArticle(NewsArticle newsArticle) {
		try {
			newsArticle.setUpdateTime(new Date());
			if (newsArticleService.updateById(newsArticle)) {
				return AjaxResultVo.success("编辑文章成功");
			} else {
				return AjaxResultVo.error("编辑文章失败");
			}
		} catch (Exception e) {
			logger.error(String.format("文章编辑失败 NewsArticleController.editArticle%s", e));
			return AjaxResultVo.error("编辑文章失败");
		}
	}

	/**
	 * 删除文章
	 * 
	 * @param newsArticleId
	 * @return
	 */
	@RequiresPermissions("newsArticle:delete")
	@PostMapping("/delete")
	@ResponseBody
	public AjaxResultVo deleteArticle(String newsArticleId) {
		try {
			if (StringUtils.isEmpty(newsArticleId)) {
				return AjaxResultVo.error("删除失败,未选择删除选项");
			}
			if (newsArticleService.removeById(newsArticleId)) {
				return AjaxResultVo.success("删除成功");
			}
			return AjaxResultVo.error("删除失败");
		} catch (Exception e) {
			logger.error(String.format("文章删除失败 NewsArticleController.deleteArticle%s", e));
			return AjaxResultVo.error("删除失败");
		}
	}

}
